package internal

import (
	"fmt"
	"math"
	"math/rand"
	"strconv"
)

func horserand_new(sortParams []int64) []ItemList {
	var (
		min           float64
		max           float64
		tDifference   float64
		randomValue   float64
		lostTimeValue float64
		data          []ItemList
	)
	min = 0.2
	max = 0.4
	tDifference = 0.5
	//min = 0.1
	//max = 0.1
	//tDifference = 0.1
	randomValue = 0
	lostTimeValue = 30
	numPartitionsOptions := []int{4, 6, 8, 10}
	data = make([]ItemList, 10)

	for i := len(sortParams) - 1; i >= 0; i-- {
		index := math.Floor(rand.Float64() * float64(len(numPartitionsOptions)))
		randomInteger := numPartitionsOptions[int(index)]
		if i < len(sortParams)-1 {
			randomValue = rand.Float64()*(max-min) + min
		}
		//lostTimeValueStr := (lostTimeValue - randomValue).toFixed(1))
		lostTimeValue, _ = strconv.ParseFloat(fmt.Sprintf("%.1f", lostTimeValue-randomValue), 64)
		tmpData := distributeNumbers(lostTimeValue, randomInteger, tDifference)
		data[i].items = append(data[i].items, tmpData...)
	}
	var arrHore []ItemList
	arrHore = make([]ItemList, 10)
	for j := 0; j < len(sortParams); j++ {
		arrHore[sortParams[j]-1] = data[j]
	}
	return arrHore
}

func distributeNumbers(targetSum float64, numParts int, tDifference float64) []Item {
	average := targetSum / float64(numParts)
	var values []Item
	var sum float64
	for i := 0; i < numParts; i++ {
		//randomOffset := parseFloat((Math.random() - tDifference).toFixed(1))
		randomOffset, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", rand.Float64()-tDifference), 64)
		partValue := math.Floor(average*10 + randomOffset*10)
		values = append(values, Item{
			t: partValue / 10,
			s: 0,
		})
		sum += partValue
	}
	adjustment := targetSum*10 - sum
	num := values[0].t*10 + adjustment
	values[0].t = num / 10
	return values
}

//func distributeNumbers(targetSum float64, numParts int, tDifference float64) []Item {
//	average := targetSum / float64(numParts)
//	var values []Item
//	var sum float64
//	//for i := 0; i < numParts; i++ {
//	//	randomOffset, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", rand.Float64()-tDifference), 64)
//	//	//randomOffset := rand.Float64()*(2*tDifference) - tDifference
//	//	partValue := math.Floor(average*10 + randomOffset*10)
//	//	values = append(values, Item{
//	//		t: partValue / 10,
//	//		s: 0,
//	//	})
//	//	sum += partValue
//	//}
//	for i := 0; i < numParts; i++ {
//		randomOffset := (rand.Float64()*(2*tDifference) - tDifference) / 10
//
//		// 计算每份的数值
//		partValue := math.Floor(average*10+randomOffset*10) / 10
//		// 将数值添加到数组中
//		//values[i] = HorseData{t: partValue}
//		values = append(values, Item{
//			t: partValue,
//			s: 0,
//		})
//		// 更新数值之和
//		sum += partValue
//	}
//	adjustment := targetSum*10 - sum
//	num := values[0].t*10 + adjustment
//	values[0].t = num / 10
//	return values
//}

//func distributeNumbers(targetSum float64, numParts int, tDifference float64) []Item {
//	// 计算平均数
//	average := float32(targetSum / float64(numParts))
//
//	// 初始化一个数组来存储每份的数值
//	values := make([]Item, numParts)
//
//	// 初始化数值之和
//	var sum float64
//
//	// 分配数值
//	for i := 0; i < numParts; i++ {
//		randomOffset := float32(rand.Float64()*(2*tDifference) - tDifference)
//
//		partValue := float64(math.Floor(float64(average*10+randomOffset*10)) / 10)
//
//		// 将数值添加到数组中
//		//values[i] = HorseData{T: partValue}
//		values = append(values, Item{
//			t: partValue,
//			s: 0,
//		})
//		// 更新数值之和
//		sum += float64(partValue)
//	}
//
//	// 调整数值，直到数值之和等于目标值
//	adjustment := targetSum*10 - sum*10
//	result := values[0].t*10 + adjustment
//	values[0].t = math.Round(result) / 10
//	return values
//}
